package com.tripwego.client;

import java.util.logging.Logger;

import com.google.gwt.activity.shared.ActivityManager;
import com.google.gwt.activity.shared.ActivityMapper;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.place.shared.Place;
import com.google.gwt.place.shared.PlaceController;
import com.google.gwt.place.shared.PlaceHistoryHandler;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.web.bindery.event.shared.EventBus;
import com.tripwego.client.common.TwgConstants;
import com.tripwego.client.mapper.AppActivityMapper;
import com.tripwego.client.mapper.AppPlaceHistoryMapper;
import com.tripwego.client.place.HomePlace;
import com.tripwego.client.ui.footer.FooterWidget;
import com.tripwego.client.ui.header.HeaderWidget;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class Tripwego implements EntryPoint {

	private static final Logger log = Logger
			.getLogger(Tripwego.class.getName());

	private FlowPanel headerPanel = new FlowPanel();
	private FlowPanel footerPanel = new FlowPanel();

	private Place defaultPlace = new HomePlace("TestHome");
	private SimplePanel appWidget = new SimplePanel();

	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {

		log.info("-onModuleLoad()-DEB");

		/* Liaison avec la page gooos.jsp */
		RootPanel.get(TwgConstants.HEADER_DIV).add(headerPanel);
		RootPanel.get(TwgConstants.FOOTER_DIV).add(footerPanel);

		ClientFactory clientFactory = GWT.create(ClientFactory.class);
		EventBus eventBus = clientFactory.getEventBus();
		PlaceController placeController = clientFactory.getPlaceController();

		// Start ActivityManager for the main widget with our ActivityMapper
		ActivityMapper activityMapper = new AppActivityMapper(clientFactory);
		ActivityManager activityManager = new ActivityManager(activityMapper,
				eventBus);
		activityManager.setDisplay(appWidget);

		// Start PlaceHistoryHandler with our PlaceHistoryMapper
		AppPlaceHistoryMapper historyMapper = GWT
				.create(AppPlaceHistoryMapper.class);
		PlaceHistoryHandler historyHandler = new PlaceHistoryHandler(
				historyMapper);
		historyHandler.register(placeController, eventBus, defaultPlace);
	
		RootPanel.get(TwgConstants.MIDDLE_DIV).add(appWidget);		
		
		headerPanel.add(new HeaderWidget());
		footerPanel.add(new FooterWidget());
		
		// Goes to the place represented on URL else default place
		historyHandler.handleCurrentHistory();

		log.info("-onModuleLoad()-FIN");
	}
}
